<?php
namespace Cron\Controller;
use Common\Model\PointLotteryModel;
use Common\Model\BusinessModel;
use Common\Model\UtilsModel;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        echo 'cli';
    }


    /**
     * 积分统计-每日
     * Author: shijy
     */
    public function point_count(){
        list($todayBegin,$todayEnd)=UtilsModel::getYesterDay();
        $data=array();
        $data['day']     = date('Ymd',$todayBegin);
        $data['stockPoint']     = 0;
        $data['todayPointAdd']  = 0;
        $data['todayPointMinus']= 0;
        $data['peopleAdd']      = 0;
        $data['peopleMinus']    = 0;
        $data['signPointAdd']   = 0;
        $data['commentPointAdd']= 0;
        $data['praisePointAdd'] = 0;
        $data['contributePointAdd']     = 0;
        $data['consumePointAdd']        = 0;
        $data['otherPointAdd']          = 0;
        $data['fullLotteryPointMinus']  = 0;
        $data['fixLotteryPointMinus']   = 0;
        $data['exchangePointMinus']     = 0;
        $data['eventsPointMinus']       = 0;
        $data['otherPointMinus']        = 0;

        $pointLog=M('u_point_log',null,DB_MAIN_CFG);

        //保有总量
        $data['stockPoint']= M('u_user_base',null,DB_MAIN_CFG)->sum('point');

        //日产出，日消耗
        $where="addTime between {$todayBegin} and {$todayEnd}";
        $sub=$pointLog
            ->where($where)
            ->group('addOrReduce')
            ->field('addOrReduce,sum(`changeVal`) as st')
            ->select();
        foreach($sub as $one){
            if($one['addOrReduce']==VAL_ADD){
                $data['dodayPointAdd']=$one['st'];
            }elseif($one['addOrReduce']==VAL_REDUCE){
                $data['dodayPointMinus']=$one['st'];
            }
        }

        //人数统计
        $sub=$pointLog
            ->where($where)
            ->group('addOrReduce')
            ->field('addOrReduce,count(distinct uid) as st')
            ->select();
        foreach($sub as $one){
            if($one['addOrReduce']==VAL_ADD){
                $data['peopleAdd']=$one['st'];
            }elseif($one['addOrReduce']==VAL_REDUCE){
                $data['peopleMinus']=$one['st'];
            }
        }

        //具体行为统计
        $sub=$pointLog
            ->where($where)
            ->group('addOrReduce,act')
            ->field('addOrReduce,sum(`changeVal`) as st')
            ->select();
        foreach($sub as $one){
            if($one['addOrReduce']==VAL_ADD){
                switch($one['act']){
                    case OPT_LOGIN://签到登陆产出
                        $data['signPointAdd']=$one['st'];
                        break;
                    case OPT_COMMENT://
                        $data['commentPointAdd']=$one['st'];
                        break;
                    case OPT_PRAISE:
                        $data['praisePointAdd']=$one['st'];
                        break;
                    case OPT_CONTRIBUTE:
                        $data['contributePointAdd']=$one['st'];
                        break;
                    default:
                        $data['otherPointAdd']+=$one['st'];
                        break;
                }

            }elseif($one['addOrReduce']==VAL_REDUCE){
                switch($one['act']){
                    case OPT_FULL_LOTTERY://
                        $data['fullLotteryPointMinus']=$one['st'];
                        break;
                    case OPT_FIX_LOTTERY:
                        $data['fixLotteryPointMinus']=$one['st'];
                        break;
                    case OPT_APPLY_POINTMALL://
                        $data['exchangePointMinus']=$one['st'];
                        break;
                    case OPT_APPLY_EVENT:
                        $data['eventsPointMinus']=$one['st'];
                        break;
                    default:
                        $data['otherPointMinus']+=$one['st'];
                        break;
                }
            }
        }
        M('stat_point_daily',null,DB_MAIN_CFG)->add($data);
    }




    /**
     * 活跃-日统计
     * Author: shijy
     */
    public function active_daily(){
        list($theBegin,$theEnd)=UtilsModel::getYesterDay();
        
        $LOGIN_TABLE='u_login_log';
        $where=" addTime between {$theBegin} and {$theEnd}";
        //用户ID列表
        $loginUserArray=M($LOGIN_TABLE,null,DB_MAIN_CFG)->where($where)->getField("distinct uid",true);
        //总活跃用户数
        $totalUser=count($loginUserArray);//M($LOGIN_TABLE,null,DB_MAIN_CFG)->where($where)->getField("count(distinct uid)");

        $where=" regTime between {$theBegin} and {$theEnd}";
        //新用户数
        $newUser=M('u_user_base',null,DB_MAIN_CFG)->where($where)->getField("count(uid)");

        //老用户数
        $oldUser=$totalUser-$newUser;

        $loss = $theBegin-UtilsModel::LOSS_TIME;
        $where=" addTime between {$loss} and {$theBegin}";//指定时间段未登陆的用户
        $activeUserArray=M($LOGIN_TABLE,null,DB_MAIN_CFG)->where($where)->getField('distinct uid',true);

        //流失用户回归
        $loseComeBack       = array_diff($loginUserArray,$activeUserArray);
        $loseComeBackCount  = count($loseComeBack);

        $newData=array(
            'timeStr'               => date('Ymd',$theBegin),
            'totalActiveUser'   => $totalUser,
            'newUser'           => $newUser,
            'oldUser'           => $oldUser,
            'lossComeback'      => $loseComeBackCount
        );
        M('stat_active_daily',null,DB_MAIN_CFG)->add($newData);

        if(count($loseComeBack)==0){
            return;
        }

        //为月统计-流失用户回归作预处理
        $LB=array();
        $month=date('Ym',$theBegin);
        foreach($loseComeBack as $uid){
            $LB[]=array(
                'timeStr'=>$month,
                'uid'=>$uid
            );
        }
        M('stat_active_monthly_pre',null,DB_MAIN_CFG)->addAll($LB,null,true);

    }

    /**
     * 活跃-月统计
     * Author: shijy
     */
    public function active_monthly(){
        list($theBegin,$theEnd)=UtilsModel::getLastMonth();

        $LOGIN_TABLE='u_login_log';
        $where=" addTime between {$theBegin} and {$theEnd}";
        //用户ID列表
        $loginUserArray=M($LOGIN_TABLE,null,DB_MAIN_CFG)->where($where)->getField("distinct uid",true);
        //总活跃用户数
        $totalUser=count($loginUserArray);

        $where=" regTime between {$theBegin} and {$theEnd}";
        //新用户数
        $newUser=M('u_user_base',null,DB_MAIN_CFG)->where($where)->getField("count(uid)");

        //老用户数
        $oldUser=$totalUser-$newUser;

        $lastMonth=date('Ym',$theBegin);

        //月流失用户回归累计，去重
         $where=" timeStr = {$lastMonth}";//指定时间段未登陆的用户
        $loseComeBackCount=M('stat_active_monthly_pre',null,DB_MAIN_CFG)->where($where)->count();

        //统计后删除无效数据
        M('stat_active_monthly_pre',null,DB_MAIN_CFG)->where($where)->delete();

        $newData=array(
            'timeStr'               => date('Ym',$theBegin),
            'totalActiveUser'   => $totalUser,
            'newUser'           => $newUser,
            'oldUser'           => $oldUser,
            'lossComeback'      => $loseComeBackCount
        );
        M('stat_active_monthly',null,DB_MAIN_CFG)->add($newData);
    }

    //积分抽奖 已结束活动是否抽奖
    public function doLottery(){
        PointLotteryModel::doLottery();
    }

    //核销员管理-月统计
    public function cardConsumeMonthly(){
        BusinessModel::monStatistic();
    }
}